package top.mccat.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.mccat.common.core.domain.bk.BkBase;
import top.mccat.common.core.domain.bk.BkSale;
import top.mccat.common.exception.bk.BaseItemNotEnoughException;
import top.mccat.common.exception.bk.BaseItemNotFoundException;
import top.mccat.common.mapper.bk.BkBaseMapper;
import top.mccat.common.mapper.bk.BkProductBaseMapper;
import top.mccat.common.mapper.bk.BkProductSaleMapper;
import top.mccat.common.mapper.bk.BkSaleMapper;
import top.mccat.common.service.IBkSaleService;

import java.beans.Expression;
import java.util.List;

/**
 * 销售记录Service业务层处理
 * 
 * @author Raven
 * @date 2023-01-10
 */
@Service
public class BkSaleServiceImpl implements IBkSaleService 
{
    @Autowired
    private BkSaleMapper bkSaleMapper;

    @Autowired
    private BkProductSaleMapper bkProductSaleMapper;

    @Autowired
    private BkProductBaseMapper bkProductBaseMapper;

    @Autowired
    private BkBaseMapper bkBaseMapper;

    /**
     * 查询销售记录
     * 
     * @param id 销售记录主键
     * @return 销售记录
     */
    @Override
    public BkSale selectBkSaleById(Integer id)
    {
        return bkSaleMapper.selectBkSaleById(id);
    }

    /**
     * 查询销售记录列表
     * 
     * @param bkSale 销售记录
     * @return 销售记录
     */
    @Override
    public List<BkSale> selectBkSaleList(BkSale bkSale)
    {
        return bkSaleMapper.selectBkSaleList(bkSale);
    }

    /**
     * 新增销售记录
     * 
     * @param bkSale 销售记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {BaseItemNotEnoughException.class,BaseItemNotFoundException.class})
    public int insertBkSale(BkSale bkSale) throws BaseItemNotEnoughException, BaseItemNotFoundException {
        int row = bkSaleMapper.insertBkSale(bkSale);
        bkProductSaleMapper.insertBkProductSale(bkSale);
        //进行删除联动
        BkBase bkBase = bkBaseMapper.selectBkBaseByProductId(bkSale.getProductId().intValue());
        if(bkBase != null){
            //进行删除，这里的接口必定是销售操作
            double value = bkBase.getAmount() - bkSale.getSaleQuantity();
            if(value < 0){
                throw new BaseItemNotEnoughException("错误，仓库内物品只有 "+bkBase.getAmount()+" 件！请确认仓库中是否有足够物品进行出库！");
            }
            bkBase.setAmount(value);
            bkBaseMapper.updateBkBase(bkBase);
        }else {
            //不存在则直接删除操作
            throw new BaseItemNotFoundException("警告！仓库中暂无该物品，无法进行销售操作！");
        }
        return row;
    }

    /**
     * 修改销售记录
     * 
     * @param bkSale 销售记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBkSale(BkSale bkSale)
    {
        int row = 0;
        //先删除关联id参数
        bkProductSaleMapper.deleteBkProductSaleByProductId(bkSale.getProductId().intValue());
        row = bkSaleMapper.updateBkSale(bkSale);
        //重新增加参数
        bkProductSaleMapper.insertBkProductSale(bkSale);
        return row;
    }

    /**
     * 批量删除销售记录
     * 
     * @param ids 需要删除的销售记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBkSaleByIds(Integer[] ids)
    {
        int row = bkSaleMapper.deleteBkSaleByIds(ids);
        bkProductSaleMapper.deleteBkProductSaleByProductIds(ids);
        return row;
    }

    /**
     * 删除销售记录信息
     * 
     * @param id 销售记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBkSaleById(Integer id)
    {
        int row = bkSaleMapper.deleteBkSaleById(id);
        bkProductSaleMapper.deleteBkProductSaleByProductId(id);
        return row;
    }
}
